package SA.teacher.manage;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import SA.teacher.*;
import SA.teacher.answer.AnswerActivity1;
import SA.teacher.daily.*;
import SA.teacher.notice.NoticeActivity1;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class ManageStudent extends Activity {
	/** Called when the activity is first created. */

	int[] barImageArray = new int[] { R.drawable.zerobar, R.drawable.tenbar,
			R.drawable.twentybar, R.drawable.thirtybar, R.drawable.fourtybar,
			R.drawable.fiftybar, R.drawable.sixtybar, R.drawable.seventybar,
			R.drawable.eightybar, R.drawable.ninetybar, R.drawable.onehundbar };

	ImageView studentPic;
	TextView studentName;
	TextView studentSchool;
	TextView studentGrade;
	TextView studentPhone;
	TextView parentPhone;
	ImageView attendBar; 
	ImageView homeworkBar; 
	TextView preComment;
	EditText newComment;
	ImageView regiBtn;
	String id;
	String rcvclassid;
	String rcvstudent;
	String shareid;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.manage2_student);

		Intent intent = getIntent();
		id = intent.getStringExtra("ID");

		shareid = intent.getStringExtra("shareID");

		studentinfoparse stups = new studentinfoparse();
		stups.run();


		final String[] studentinfo = rcvstudent.split(",");
		TextView className = (TextView) findViewById(R.id.className);
		className.setText(studentinfo[0] + " 占쏙옙");

		studentPic = (ImageView) findViewById(R.id.studentpic);
		studentName = (TextView)findViewById(R.id.studentname);
		studentName.setText(studentinfo[1]);
		studentSchool = (TextView)findViewById(R.id.studentschool);
		studentSchool.setText(studentinfo[2]+"占싻깍옙");
		studentGrade = (TextView)findViewById(R.id.studentgrade);
		studentGrade.setText( studentinfo[3]+ "占싻놂옙");
		studentPhone = (TextView)findViewById(R.id.studentphone);
		studentPhone.setText(studentinfo[4]);

		parentPhone = (TextView)findViewById(R.id.parentphone);

		attendBar = (ImageView) findViewById(R.id.attendbar);
		homeworkBar = (ImageView) findViewById(R.id.homeworkbar);
		preComment = (TextView) findViewById(R.id.precomment);
		newComment = (EditText) findViewById(R.id.newcomment);
		regiBtn = (ImageView) findViewById(R.id.regibtn);

		//////////////Button action////////////////////////////////////
		TextView noticeBtn = (TextView) findViewById(R.id.notice);
		noticeBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				noticeActivity();
			}
		});

		ImageView mainBtn = (ImageView) findViewById(R.id.mainBtn);
		mainBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				mainActivity();
			}
		});

		ImageView manageBtn = (ImageView) findViewById(R.id.manageBtn);
		manageBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				manageActivity();
			}
		});

		ImageView dailyBtn = (ImageView) findViewById(R.id.dailyBtn);
		dailyBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				dailyActivity();
			}
		});
		ImageView answerBtn = (ImageView) findViewById(R.id.answerBtn);
		answerBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				answerActivity();	
			}
		});
	}

	private class classidparse implements Runnable {
		public void run() {	Log.e("login 占쏙옙占쏙옙 : ", id);
		// TODO Auto-generated method stub
		try {
			// String addr
			// ="Http://220.67.128.8/temp.php?useUnicode=true&characterEncoding=EUC-KR&tID=1234"
			// + "&adate=20120902" + "&classid="+ classid +
			// "&content="+content;

			URL url = new URL("Http://220.67.128.8/dayclassid.php?id="
					+ URLEncoder.encode(id, "EUC-KR"));
			// url.openStream();
			// URL url = new URL(addr);

			HttpURLConnection conn = (HttpURLConnection) url
					.openConnection();

			InputStream is = null;
			ByteArrayOutputStream baos = null;
			if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
				is = conn.getInputStream();
				baos = new ByteArrayOutputStream();
				byte[] byteBuffer = new byte[1024];
				byte[] byteData = null;
				int nLength = 0;
				while ((nLength = is.read(byteBuffer)) > 0) {
					baos.write(byteBuffer, 0, nLength);
				}
				byteData = baos.toByteArray();
				String response = new String(byteData, "euc-k-r");
				rcvclassid = response;
				if (response == null || response.equals("")) {
					throw new Exception("Response is NULL!");
				}
			} else {
				Log.e("login 占쏙옙占쏙옙 : ", "占쏙옙占쏙옙 占싫듸옙");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
		}
		}
	}
	private class studentinfoparse implements Runnable {
		public void run() {	
			// TODO Auto-generated method stub
			try {URL url = new URL("Http://220.67.128.8/studentinfo.php?id="+ URLEncoder.encode(shareid, "EUC-KR"));
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();

			InputStream is = null;
			ByteArrayOutputStream baos = null;
			if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
				is = conn.getInputStream();
				baos = new ByteArrayOutputStream();
				byte[] byteBuffer = new byte[1024];
				byte[] byteData = null;
				int nLength = 0;
				while ((nLength = is.read(byteBuffer)) > 0) {
					baos.write(byteBuffer, 0, nLength);
				}
				byteData = baos.toByteArray();
				String response = new String(byteData, "euc-k-r");
				rcvstudent = response;
				if (response == null || response.equals("")) {
					throw new Exception("Response is NULL!");
				}
			} else {
				Log.e("login 占쏙옙占쏙옙 : ", "占쏙옙占쏙옙 占싫듸옙");
			}
			} catch (Exception e) {
				// TODO Auto-generated catch block
			}
		}
	}
	//////////////Button �⑥닔��///////////////////////////////////
	void noticeActivity() {
		Intent intent = new Intent(this, NoticeActivity1.class);
		intent.putExtra("ID",id);
		startActivity(intent);
	}
	void mainActivity() {
		Intent intent = new Intent(this, MainActivity.class);
		intent.putExtra("ID",id);
		startActivity(intent);
	}
	void manageActivity() {
		classidparse cidps = new classidparse();
		cidps.run();
		final String[] classes = rcvclassid.split(",");
		if(classes.length-1!=0){
			new AlertDialog.Builder(ManageStudent.this)
			.setTitle("諛섏쓣 �좏깮�댁＜�몄슂.")
			.setItems(classes, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog,	int which) {
					Intent intent = new Intent(ManageStudent.this, ManageActivity1.class);
					intent.putExtra("ID",id);
					intent.putExtra("classname",classes[which]);
					startActivity(intent);
				}
			}).show();
		}
		else{
			new AlertDialog.Builder(ManageStudent.this)
			.setMessage("�꾩옱 �대떦�섎뒗 諛섏씠 �놁뒿�덈떎.")
			.setPositiveButton("�뺤씤", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
					// TODO Auto-generated method stub
				}
			}).show();
		}
	}
	void dailyActivity() {
		Intent intent = new Intent(this, DailyActivity1.class);
		intent.putExtra("ID",id);
		startActivity(intent);
	}
	void answerActivity() {
		Intent intent = new Intent(this, AnswerActivity1.class);
		intent.putExtra("ID",id);
		startActivity(intent);
	}

	
}
